/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.graphs.io;

import java.util.HashMap;
import java.util.Map;
import org.osdtsystem.exceptions.InconsistentGraphException;
import org.osdtsystem.graphs.Graph;
import org.osdtsystem.graphs.views.TreeEngine;
import org.osdtsystem.graphs.views.View;
import org.osdtsystem.graphs.views.ViewID;
import org.osdtsystem.utils.Helper;
import org.osdtsystem.values.Layer;
import org.osdtsystem.values.NodeID;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public class GraphIDs {
    private Graph graph;
    private Map<Object,Integer> ids = new HashMap<Object,Integer>();
    private Map<Integer,Layer> layers = new HashMap<Integer,Layer>();
    private Map<Integer,View> views = new HashMap<Integer,View>();
    private Map<Integer,NodeID> nodes = new HashMap<Integer,NodeID>();
    
    private Integer nextlayer = 0;
    private Integer nextnode = 0;
    private Integer nextview = 0;
    
    public GraphIDs(Graph graph) {
        // Save graph
        this.graph = graph;
    }
    
    public void compute() {
        // Number views
        for (ViewID viewID : graph.views()) {
            View view = graph.view(viewID);
            if (view != null) 
                add(graph.view(viewID));
            else
                throw new InconsistentGraphException("Cannot find view associated with view ID "
                        + viewID);
        }

        // Number layers
        for (Layer layer : Helper.sort(graph.layers()))
            add(layer);

        // Number nodes
        for (NodeID node : Helper.sort(graph.nodes()))
            add(node);
    }

    public Layer getLayer(Integer id) {
        return layers.get(id);
    }

    public Layer getLayer(String id) {
        return layers.get(Integer.parseInt(id));
    }

    public View getView(Integer id) {
        return views.get(id);
    }
    
    public View getView(String id) {
        return views.get(Integer.parseInt(id));
    }
    
    public NodeID getNode(Integer id) {
        return nodes.get(id);
    }
    
    public NodeID getNode(String id) {
        return nodes.get(Integer.parseInt(id));
    }
    
    public Integer id(Object object) {
        return ids.get(object);
    }

    public Integer add(Layer layer, Integer id) {
        ids.put(layer, id);
        layers.put(id, layer);
        return id;
    }
    
    public Integer add(View view, Integer id) {
        ids.put(view, id);
        views.put(id, view);
        return id;
    }
        
    public Integer add(NodeID node, Integer id) {
        ids.put(node, id);
        nodes.put(id, node);
        return id;
    }

    public Integer add(Layer layer) {
        Integer id = ids.get(layer);
        return id != null ? id : add(layer, nextlayer++);
    } 
    
    public Integer add(View view) {
        Integer id = ids.get(view);
        return id != null ? id : add(view, nextview++);
    } 
    
    public Integer add(NodeID node) {
        Integer id = ids.get(node);
        return id != null ? id : add(node, nextnode++);
    } 
    

}
